$(document).ready(function(){
	
});

var FOLD = 0;
var CHECK = 1;
var CALL = 2;
var RAISE = 3;
var ALLIN = 4;
var ALLINRAISE = 5; // mängija läheb ALLIN, ning seeläbi raiseb

function Player(id, name, money){
	this.id = id;
	this.name = name;
	this.money = money;	
	this.hand = new Hand();
	this.bet = 0; //mängija kogusumma pot'is
	this.isHuman = false;
	this.fold = fold;
	this.checkCall = checkCall;
	this.raise = raise;
	this.getPlayerHand = getPlayerHand;
	this.action = -1; // vastavalt mängija otsusele, omandab see muutuja kas FOLD, CHECK, CALL või RAISE väärtuse
	this.endTurn = endTurn; //meetod, mis käivitatakse pärast mängija otsuse tegemist
	//meetodis uuendatakse mänguseis vastavalt mängija tehtud otsusele
	this.isAllIn = false;
	this.goAllIn = goAllIn;
}

function fold(){
	//eemaldab mängija kes on foldinud
	
	this.bet = 0;
	console.log("FOLD " + this.name);
	this.action = FOLD;
	hidePlayerCards(this.id);
	this.endTurn(FOLD);
}


function checkCall(){
	if (game.playersBets.max() - game.playersBets[this.id] > 0) { 
		// kui keegi on round'is panustanud rohkem, kui sina, siis on sul võimalik callida
		var toCall;
		if (game.playersBets.max() - game.playersBets[this.id] >= this.money){
			toCall = this.money;
			this.money = 0;
			this.goAllIn(game.playersBets[this.id] + toCall);
			this.action = ALLIN;
		}else {
			toCall = game.playersBets.max() - game.playersBets[this.id];
			this.money -= game.playersBets.max() - game.playersBets[this.id];
			console.log("CALL " + this.name + ", $" + (toCall));
			this.action = CALL;
		}
		game.pot += toCall;
		game.playersBets[this.id] += (toCall);
		this.bet += game.playersBets[this.id];
		addPlayerBet(this,game.playersBets[this.id]);
	}
	else {
		console.log("CHECK " + this.name);
		this.action = CHECK;
	}
	if (!this.isAllIn){
		this.endTurn(CALL);
	} else {
		this.endTurn(ALLIN);
	}
}
//
function raise(){
	if (game.betCounter < game.maxBets){
		var raise = (game.maxBetAmount + game.playersBets.max()) - game.playersBets[this.id];
		if (raise >= this.money) {
			// kui järgmine raise on suurem või võrdne, kui mängijal raha on, siis mängija ALLIN
			if (raise == this.money){
				game.betCounter += 1;
			}
			raise = this.money;
			this.money = 0;
			game.pot += raise;
			prevBetsMax = game.playersBets.max();
			game.playersBets[this.id] += raise;
			this.goAllIn(game.playersBets[this.id]);
			this.bet += game.playersBets[this.id];
			addPlayerBet(this, game.playersBets[this.id]);
			this.action = ALLIN;
			if (game.playersBets[this.id] > prevBetsMax){ 
				//kui raise on eelmistest panustest suurem
				this.endTurn(ALLINRAISE);
			}
			else {
				this.endTurn(ALLIN);
			}
		}
		else {
			this.money -= raise;
			game.betCounter += 1;
			game.playersBets[this.id] += raise;
			console.log("RAISE " + this.name + ", to $" + game.playersBets[this.id]);
			game.pot += raise;
			
			this.bet += game.playersBets[this.id];
			addPlayerBet(this, game.playersBets[this.id]);
			this.action = RAISE;
			this.endTurn(RAISE);
		}
		
		
	}
}

function goAllIn(amount){
	console.log("ALL IN " + this.name + ", $" + (amount));
	this.isAllIn = true;
	game.allInPlayers.push(this);
}

function getPlayerHand(){
	return this.hand.cards[0].getCardName() + ", " + this.hand.cards[1].getCardName();
}

function endTurn(action){	
	if (game.currentPlayers.indexOf(this)+1 < game.currentPlayers.length){
		// võtame järgmise mängija aktiivseks mängijaks
		game.activePlayer = game.currentPlayers[game.currentPlayers.indexOf(this)+1];
	}else {
		game.activePlayer = game.currentPlayers[0];
	}
	
	if (action == RAISE){
		// tõstame järjekorra ümber, viimane, kes räägib, on tõstjale eelnev mängija
		game.currentPlayers = game.currentPlayers.slice(game.currentPlayers.indexOf(this)+1,game.currentPlayers.lenght).concat(game.currentPlayers.slice(0,game.currentPlayers.indexOf(this)+1));
		game.lastPlayerToAct = game.currentPlayers[game.currentPlayers.length-2];
		if (this == game.lastPlayerToAct){
			// kui oleme järjendi lõpus, siis järgmine mängija on järjendi esimene mängija
			game.activePlayer = game.currentPlayers[0];
			console.log(game.activePlayer.name);
		}
	}
	
	if (action == FOLD || action == ALLIN){
		// eemaldame mängija antud käe mängijate järjendist
		game.currentPlayers.splice(game.currentPlayers.indexOf(this),1);
		game.currentPlayersUnordered.splice(game.currentPlayersUnordered.indexOf(this),1);
	}
	
	if (action == ALLINRAISE){ // kui mängija raiseb ning läheb sellega ALLIN
		game.currentPlayers = game.currentPlayers.slice(game.currentPlayers.indexOf(this)+1,game.currentPlayers.lenght).concat(game.currentPlayers.slice(0,game.currentPlayers.indexOf(this)+1));
		game.lastPlayerToAct = game.currentPlayers[game.currentPlayers.length-2];
		game.activePlayer = game.currentPlayers[0];
		//tõstame järjekorra ümber ja eemaldame ALLIN mängija järjekorrast
		game.currentPlayers.splice(game.currentPlayers.indexOf(this),1);
		game.currentPlayersUnordered.splice(game.currentPlayersUnordered.indexOf(this),1);
	}
	
	if (game.currentPlayers.length == 1 && (game.allInPlayers.length == 0)){
		//kui jäänud on üks panustaja (panustaja võib olla ALLIN)
		game.winnerIs([game.currentPlayers[0]]);
	}
	else if ((game.currentPlayers.length == 0 && (game.allInPlayers.length == 1))){
		game.winnerIs([game.allInPlayers[0]]);
	}
	else if ((this == game.lastPlayerToAct && (action == CALL || action == FOLD || action == ALLIN)) || (game.currentPlayers.length == 0  && game.allInPlayers.length > 1)){ 
		//kui kord on viimase mängija käes, ning ta CALLib või FOLDib, siis jätkame mänguga
		if (game.currentPlayers.length < 2 && game.allInPlayers.length >= 1){
			while (NEXTPHASE<SHOWDOWN){ //mäng showdownini välja
				game.start(NEXTPHASE);
			}
			setTimeout("game.start("+NEXTPHASE+")",2000);
		}else {
			game.start(NEXTPHASE);
		}
		
	}
	
	if (game.currentPlayers.length > 0){
		if (!game.activePlayer.isHuman && NEXTPHASE != PREFLOPGAME){
			setTimeout("ai.getAction(game.activePlayer)",1000);
		}
	}
	updatePlayerDetails(this);
	game.updateUI();
}
Array.prototype.max = function(){
	var max = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++){
		if (this[i] > max){
			max = this[i];
		}
	}
	return max;
};